package com.shopmall.controller;

import com.shopmall.pojo.Admin;
import com.shopmall.pojo.AdminRecord;
import com.shopmall.pojo.User;
import com.shopmall.service.AdminRecordServiceImpl;
import com.shopmall.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;


@Controller
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private AdminRecordServiceImpl adminRecordService;

    //测试代码段
    /*
    @GetMapping("/queryUserById/{id}") //当地址栏跳到某个页面时，返回这个结果
    public User queryUserById(@PathVariable("id") int id){
        return userService.queryUserById(id);
    }

    @GetMapping("/queryUserList")
    public List<User> queryUserList(){
        return userService.queryUserList();
    }

    @GetMapping("/queryUserByUsername/{username}")
    public User queryUserByUsername(@PathVariable("username") String username){
        return userService.queryUserByUsername(username);
    }

    @PostMapping("/updateUser")
    public int updateUser(User user){  //通过反射机制，自动将多个参数绑定到pojo实体类中建对象
        return userService.addUser(user);
    }

    @GetMapping("/deleteUser/{id}")
    public int deleteUser(@PathVariable int id){
        return userService.deleteUser(id);
    }

     */

    //实际功能代码
    @RequestMapping("/user/user_show")
    public String queryUserMallList(Model model,HttpSession session){
        Admin account = (Admin) session.getAttribute("admin");
        if(account.getRole()==0 || account.getRole()==1) {
            List<User> userList = userService.queryUserList();
            model.addAttribute("users", userList);
            return "user/user_show";
        }
        else {
            model.addAttribute("msg","权限不足！");
            return "/home";
        }
    }

    @RequestMapping("/user/add_user")
    public String add_user(HttpSession session, Model model){
        Admin account = (Admin) session.getAttribute("admin");
        if(account.getRole()==0 || account.getRole()==1) {
            return "user/add_user";
        }
        else {
            model.addAttribute("msg","权限不足！");
            return "/home";
        }
    }

    @RequestMapping("/user/add_user_do")
    public String add_user_do(User user, Model model,HttpSession session) {
        Admin account = (Admin) session.getAttribute("admin");
        if(account.getRole()==0 || account.getRole()==1) {
            User user2 = userService.queryUserByUsername(user.getUsername());
            if (user2 != null) {
                model.addAttribute("msg", "已有相同账号的用户，添加失败！");
                return "home";
            } else {
                Timestamp date = new Timestamp(System.currentTimeMillis());
                user.setAdd_date(date);
                user.setUpdate_date(date);
                user.setState(1);
                userService.addUser(user);

                model.addAttribute("msg", "添加用户成功！");
                return "redirect:/user/user_show";

            }
        }
        else {
            model.addAttribute("msg","权限不足！");
            return "/home";
        }
    }

    @GetMapping("/user/modify_user/{id}")
    public String modify_user(@PathVariable("id") int id,Model model,HttpSession session){
        Admin account = (Admin) session.getAttribute("admin");
        if(account.getRole()==0 || account.getRole()==1) {
            User user = userService.queryUserById(id);
            model.addAttribute("user", user);
            return "user/modify_user";
        }

        else {
            model.addAttribute("msg","权限不足！");
            return "/home";
        }
    }

    @RequestMapping("/user/modify_user_do")
    public String modify_user_do(User user, Model model, HttpSession session) {
        Admin account = (Admin) session.getAttribute("admin");

        if (account.getRole() == 0 || account.getRole() == 1) {
            Timestamp date = new Timestamp(System.currentTimeMillis());
            AdminRecord record = new AdminRecord();
            record.setDo_who(account.getUsername());
            record.setDo_time(date);
            int state = account.getState();
            String string1;
            if (state == 0) {
                string1 = "超级管理员";
            } else if (state == 1) {
                string1 = "系统管理员";
            } else {
                string1 = "商品管理员";
            }
            record.setDo_role(string1);
            record.setDone_who(user.getUsername());
            record.setDone_role("用户");
            record.setDo_what("修改用户信息");
            //以上是记录信息设置
            userService.updateUser(user);
            adminRecordService.addRecord(record);
            model.addAttribute("msg", "成功修改用户 " + user.getUser_id() + user.getUsername() + " 的信息！");
            return "/home";
        } else {
            model.addAttribute("msg", "权限不足！");
            return "/home";
        }

    }
}
